package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.utils.CurrentUserUtil;
import com.example.dao.HierarchicDao;
import com.example.entity.domain.Hierarchy;
import com.example.entity.dto.BaseDTO;
import com.example.service.HierarchicService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Author: zhuxiaokang
 * @CreateTime: 2025-05-07
 * @Description: 分级
 * @Version: 1.0
 */

@Service
public class HierarchicServiceImpl implements HierarchicService {

    @Resource
    private HierarchicDao hierarchicDao;


    @Override
    public Page<Hierarchy> list(BaseDTO baseDTO) {
        Page<Hierarchy> page = new Page<>(baseDTO.getPageNum(), baseDTO.getPageSize());
        return hierarchicDao.pageList(page, baseDTO);
    }

    @Override
    public Integer current() {
        LambdaQueryWrapper<Hierarchy> queryWrapper = new LambdaQueryWrapper<>();
        return hierarchicDao.selectCount(queryWrapper).intValue();
    }

    @Override
    public Boolean add(Hierarchy hierarchy) {
        String username = CurrentUserUtil.getCurrentUsername();
        hierarchy.setCreatedBy(username);
        hierarchy.setUpdatedBy(username);
        hierarchy.setCreatedTime(LocalDateTime.now());
        hierarchy.setUpdatedTime(LocalDateTime.now());
        return hierarchicDao.insert(hierarchy) > 0;
    }

    @Override
    public Boolean delete(Integer id) {
        return hierarchicDao.deleteById(id) > 0;
    }

    @Override
    public Boolean update(Hierarchy hierarchy) {
        String username = CurrentUserUtil.getCurrentUsername();
        hierarchy.setUpdatedBy(username);
        hierarchy.setUpdatedTime(LocalDateTime.now());
        return hierarchicDao.updateById(hierarchy) > 0;
    }
}
